package com.wwq.websocket.demo2.websocketdemo2.service;

import com.wwq.websocket.demo2.websocketdemo2.viewmodel.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.stream.IntStream;

@Service
public class WebSocketService {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    public void sendMessage() {
        IntStream.range(0, 10).forEach(a -> {
            try {
                Thread.sleep(1500);
                simpMessagingTemplate.convertAndSend("/topic/getResponse", new ResponseMessage("welcome user " + a));
                System.out.println("send to " + a);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
